/**
 * 作者：陈圳佳
 * 日期：2015/8/11
 */
var pjaxObj = {cache: false, timeout: 50000};
//定义一个全局的对象给Pjax的回调方法调用
var globalObj = {
    callback: function () {
    },
    successCall: function (resp) {
    },
    message: null,
    getMessage: function (msg, type) {
        if (this.message == null)
            this.message = Messenger().post({showCloseButton: true, message: msg, type: type, hideAfter: 1});
        else
            this.message = this.message.update({type: type, message: msg});
    }
};
function showErrorMessage(msg) {
    globalObj.getMessage(msg, "error");
}
function showMessage(msg) {
    globalObj.getMessage(msg, "success");
}
$(function () {
    Messenger.options = {
        extraClasses: 'messenger-fixed messenger-on-bottom messenger-on-right',
        theme: 'flat'
    };
    $("a[data-pjax]").pjax(pjaxObj).click(function () {
        var $a = $("a[data-pjax]");
        $a.find("div").remove();
        $a.parent().removeClass("active");
        var html = "<div class='pointer'><div class='arrow'></div><div class='arrow_border'></div></div>";
        if ($(this).attr("href").indexOf("home") > 0) {
            $(this).parent().addClass("active");
        }
        $(this).append(html);
    });
    $(document).on('pjax:start', function () {
        $("#sidebar-nav").slideUp(200);
        $("#content").hide();
        $('#load-dialog').modal('show');
    }).on("pjax:end", function () {
        var $content = $("#content");
        $content.find("a[data-pjax]").pjax(pjaxObj);
        setTimeout(function () {
            $('#load-dialog').modal('hide');
            $("#sidebar-nav").slideDown(400);
            $content.show(400);
        }, 200);
    }).on('pjax:complete', function (e) {
        var elem = angular.element("div#content");
        var scope = elem.scope();
        var $compile = elem.injector().get('$compile');
        var html = $compile($(e.target).html())(scope);
        elem.html(html);
        globalObj.callback();
        scope.$digest();
    });
    $('#edit-dialog').modal({
        keyboard: false,
        show: false
    }).on("hidden.bs.modal", function (e) {
        $.pjax({url: location.href, container: '#content'});
        $('#edit-dialog').find("#edit-dialog-content").children().detach();
    });

// sidebar menu dropdown toggle
    $("#dashboard-menu").find(".dropdown-toggle").click(function () {
        var $item = $(this).parent();
        $item.toggleClass("active");
        var $i = $(this).find("i");
        if ($item.hasClass("active")) {
            $item.find(".submenu").slideDown("fast");
            iocnUp($i);
        } else {
            $item.find(".submenu").slideUp("fast");
            iocnDown($i);
        }
    });


// mobile side-menu slide toggler
    var $menu = $("#sidebar-nav");
    $("body").click(function () {
        if ($menu.hasClass("display")) {
            $menu.removeClass("display");
        }
    });
    $menu.click(function (e) {
        e.stopPropagation();
    });
    $("#menu-toggler").click(function (e) {
        e.stopPropagation();
        $menu.toggleClass("display");
    });
    $.pjax({url: params, container: '#content'});
});
function showEditDialog(url) {
    $("#edit-dialog-content").load(url, function (resp) {
        var $dialog = $("#edit-dialog-content");
        var elem = angular.element("div#edit-dialog-content");
        var $compile = elem.injector().get('$compile');
        var scope = elem.scope();
        elem.html($compile($dialog.html())(scope));
        scope.$digest();
        $("#edit-dialog").modal("show");
    });
}
function iocnUp(i) {
    i.removeClass("glyphicon glyphicon-chevron-down");
    i.addClass("glyphicon glyphicon-chevron-up");
}
function iocnDown(i) {
    i.removeClass("glyphicon glyphicon-chevron-up");
    i.addClass("glyphicon glyphicon-chevron-down");
}
/**
 * 保存Cookie为7天
 */
function setCookie(name, value) {
    var d = new Date();
    d.setTime(d.getTime() + (7 * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = name + "=" + value + "; " + expires;
}
/**
 *根据名字拿到Cookie
 */
function getCookie(name) {
    name = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}
function removeCookie(name) {
    /* -1 天后过期即删除 */
    setCookie(name, 1, -1);
}
//获取当前日期
function getDate() {
    var d = new Date();
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();
    var h = d.getHours();
    var m = d.getMinutes();
    //var s = d.getSeconds();
    return year + "年" + month + "月" + day + "日 " + h + ":" + m;
}
/**
 * 获取地址栏的参数。
 * @param name 参数名。
 * @returns {null}
 * @constructor
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}
function getMangePath() {
    var href = location.href;
    return href.substr(0, href.indexOf("manage") + "manage".length);
}